<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >
    <title>postMessage A</title>
</head>

<body>

    <iframe
        id="iframe"
        src="http://127.0.0.1:10002/B.html"
        frameborder="0"
        style="display: none;"
    ></iframe>
    <script>
        iframe.onload = function () {
            // 向B发送数据
            iframe.contentWindow.postMessage('Hello! this is A.html', 'http://127.0.0.1:10002/');
        }
        // 监听B发送过来的数据
        window.onmessage = function (ev) {
            console.log('A打印B发送的数据: ' + ev.data);
            // ev.source.postMessage(ev.data + '@@', ev.origin);
        }
    </script>
</body>

</html>